package extend_widget

import (
	"clientDemo/ui/fyne_ui/common"
	"clientDemo/ui/fyne_ui/widget"
	"clientDemo/ui/fyne_ui/widget/interface"
	"clientDemo/utils"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"image/color"
)

func init() {
	widget.RegWidget("WidgetTxt", "普通文本", NewNewLabel)
}

type NewText struct {
	*WidgetContainer
	Text *canvas.Text
	*Action
	rootNode widgetcommon.IWidgetItem //父节点
	name     string
	scale    float32
	textSize float32
}

func NewNewLabel(name string, skinInfo widgetcommon.IWidgetSkin, rootNode widgetcommon.IWidgetItem) widgetcommon.IWidgetItem {
	if skinInfo == nil {
		return nil
	}
	o := &canvas.Text{}
	_label := &NewText{
		Text:   o,
		Action: &Action{},
		WidgetContainer: &WidgetContainer{
			Container: &Container{
				name: name,
			},
			Border: &canvas.Rectangle{
				FillColor:   color.Transparent,
				StrokeColor: color.RGBA{255, 0, 0, 255}, //黄色
				StrokeWidth: 2,
			},
		},
		rootNode: rootNode,
	}
	_label.WidgetContainer.ac = _label.Action
	_label.IUserInfo = common.NewWidgetUserInfo(name, skinInfo.GetSkinName(), _label)
	_label.WidgetContainer.SetObjects([]fyne.CanvasObject{_label.Text})
	_label.SetBase(_label, rootNode)
	_label.ExtendBaseWidget(_label)
	_label.name = name
	_label.scale = 1.0
	_label.textSize = 1.0
	return _label
}
func (l *NewText) SetProperty(key, value string, isProperty bool) {
	l.WidgetContainer.SetProperty(key, value, isProperty)
	switch key {
	case "Scale":
		l.scale = utils.ParseFloat32(value)
		l.Text.TextSize = l.textSize * l.scale
	case "ShowTxt":
		l.Text.Text = value
	case "FontSize":
		l.textSize = utils.ParseFloat32(value)
		l.Text.TextSize = l.textSize
	case "Color":
		color1, err := utils.ParseColor(value)
		if err == nil {
			l.Text.Color = color1
		}
	}
}

func (l *NewText) MinSize() fyne.Size {
	return l.Text.MinSize()
	//i.ExtendBaseWidget(i)
	//return i.BaseWidget.MinSize()
}

/*
type CustomLabel struct {
	text   *canvas.Text
	Border *canvas.Rectangle // 边框
}

func NewCustomLabel(name string) *CustomLabel {
	text := canvas.NewText(name, color.Black) // 使用 canvas.Text
	Border := canvas.NewRectangle(color.Transparent)

	return &CustomLabel{
		text:   text,
		Border: Border,
	}
}

func (l *CustomLabel) SetText(newText string) {
	l.text.Text = newText
	l.text.Refresh() // 刷新文本
}

func (l *CustomLabel) SetFontSize(size float32) {
	l.text.TextSize = size
	l.text.Refresh() // 刷新文本以应用新字体大小
}

func (l *CustomLabel) Layout(size fyne.Size) {
	l.Border.Resize(size)
	l.Border.Move(fyne.NewPos(0, 0))
	l.text.Resize(size)
	l.text.Move(fyne.NewPos(0, 0))
}
*/
